import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface Wallet {
  balance: number;
  totalRecharge: number;
  totalConsume: number;
}

interface PendingOrder {
  orderNo: string | null;
  productType: string;
  productId: string | null;
  amount: number;
}

interface PaymentState {
  wallet: Wallet;
  pendingOrder: PendingOrder;
  setWallet: (wallet: Wallet) => void;
  updateBalance: (balance: number) => void;
  setPendingOrder: (order: PendingOrder) => void;
  clearPendingOrder: () => void;
}

const usePaymentStore = create<PaymentState>()(
  persist(
    (set) => ({
      wallet: {
        balance: 0,
        totalRecharge: 0,
        totalConsume: 0
      },
      pendingOrder: {
        orderNo: null,
        productType: '',
        productId: null,
        amount: 0
      },
      setWallet: (wallet) => set({ wallet }),
      updateBalance: (balance) => set((state) => ({
        wallet: { ...state.wallet, balance }
      })),
      setPendingOrder: (order) => set({ pendingOrder: order }),
      clearPendingOrder: () => set({
        pendingOrder: {
          orderNo: null,
          productType: '',
          productId: null,
          amount: 0
        }
      })
    }),
    {
      name: 'payment-storage',
      partialize: (state) => ({
        wallet: state.wallet,
        pendingOrder: state.pendingOrder
      })
    }
  )
);

export default usePaymentStore;
